package com.example.red_tea_house.moudle.Home.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.red_tea_house.R;
import com.example.red_tea_house.entity.StoryBean;
import com.example.red_tea_house.moudle.Home.view.ReadFragment;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class StoryAdapter extends RecyclerView.Adapter<StoryAdapter.articleViewHolder> {
    private Context context;
    private View view;
    private List<StoryBean> data;
    private ReadFragment readFragment;


    public StoryAdapter(List<StoryBean> data, Context context,ReadFragment readFragment){//used in readFragment
        this.data=data;
        this.context=context;
        this.readFragment=readFragment;
    }
    public StoryAdapter(List<StoryBean> data, Context context){//used in WriteFragment
        this.data=data;
        this.context=context;
    }

    @NotNull
    @Override
    public StoryAdapter.articleViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        view= LayoutInflater.from(context).inflate(R.layout.fragment_story_item,parent,false);
        return new articleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull StoryAdapter.articleViewHolder holder, int position) {
            holder.titleView.setText(data.get(position).getTitle());
            holder.contentView.setText(data.get(position).getContent());
            holder.timeView.setText(data.get(position).getTime());
    }


    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public class articleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        TextView contentView;
        TextView titleView;
        TextView timeView;

        public articleViewHolder(@NonNull View itemView) {
            super(itemView);
            contentView=view.findViewById(R.id.tv_story_content);
            titleView =view.findViewById(R.id.tv_story_title);
            timeView=view.findViewById(R.id.tv_story_time);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            //readFragment.turnPage();
        }
    }
}
